package rc.database.connection;

/**
 * Created by IntelliJ IDEA.
 * User: Slejus
 * Date: 13.11.11
 * Time: 14.35
 * To change this template use File | Settings | File Templates.
 */
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class MyDBOperations
{
    private static final Logger logger = Logger.getLogger(MyDBOperations.class);

    private String username;
    private String password;
    private String jdbcUrl;
    private String jdbcDriver;

    public MyDBOperations(String usn, String pass, String url, String driver) {
        this.username = usn;
        this.password = pass;
        this.jdbcUrl = url;
        this.jdbcDriver = driver;
    }
    
    public String getUserName() {
        return this.username;
    }
    
    public String getPassword() {
        return this.password;
    }

    public String getJdbcUrl() {
        return this.jdbcUrl;
    }

    public String getJdbcDriver() {
        return this.jdbcDriver;
    }
    
    public String getAlternativeUsers(int var_id) {
        String query = "SELECT a.var_id, a.var_vardas, a.var_pavarde FROM jussle.vartotojai a " +
                "WHERE a.padn_id = jussle.getsuperiorpad("+var_id+") and a.var_id != "+var_id+" and a.var_id != jussle.getsuperior("+var_id+")";
        logger.debug("QUERY: " +query);
        return query;
    }
    
    public String getTarnNrAndPadPav(int amn_id){
        String query = "SELECT a.padn_pav as TAR_PAD, b.padn_pav as PAD "+
                "FROM jussle.padaliniai a, jussle.padaliniai b "+
                "WHERE a.padn_id = "+
                "(SELECT c.tarn_nr FROM jussle.vartotojai c WHERE c.var_id="+amn_id+") and "+
                "b.padn_id = (SELECT d.padn_id FROM jussle.vartotojai d WHERE d.var_id="+amn_id+")";
        logger.debug("QUERY: " +query);
        return query;
    }

    /*
    public String getSuperior(int var_id)
    {
        String query = "SELECT jussle.getSuperior("+var_id+") FROM dual";
        logger.debug("QUERY: " +query);
        return query;
    }
    */
    
    public String getSuperior(int var_id) {
        String query = "SELECT  a.var_id, a.var_vardas, a.var_pavarde "+
                "FROM jussle.vartotojai a WHERE a.var_id = jussle.getsuperior("+var_id+")";
        logger.debug("QUERY: " +query);
        return query;
    }


    public List RsIntToList(ResultSet rs) {
        List<String> list = new ArrayList<String>();
        try {
            while (rs.next()) {
                list.add(Integer.toString(rs.getInt(1)));
            }
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return list;
    }

}